package Main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Client extends Thread {

	public Catalog catalog;
	public String clientName;
	public ArrayList<Request> requests;
	
	public Client(Catalog c, String n)
	{
		catalog=c;
		clientName=n;
		requests=new ArrayList<Request>();
		loadRequestFromFile(clientName);
	}
	
	public void loadRequestFromFile(String clientRequestFile)
	{

		try {
			
			Scanner fileScanner = new Scanner(new FileInputStream(clientRequestFile + ".txt"));
			
			while(fileScanner.hasNext())
			{
				String line=fileScanner.nextLine();
				String[] splits=line.split("/");
				int hall = Integer.parseInt(splits[3]);
				int seats=Integer.parseInt(splits[6]);
				Request readrequest=new Request(splits[0],splits[1],splits[2],hall,splits[4],splits[5],seats);
				requests.add(readrequest);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run()
	{
		
		for(Request r: requests)
		{
			Event found;
			found=catalog.findEvent(r.eventName, r.hallID, r.requestDate, r.requestTime);
			if(found!= null)
			{
				found.book(r.firstName, r.lastName, r.requestSeats);
			}
		}
	}
	
	public String toString()
	{
		String str="Client: " + clientName + "\n";
		for(Request r : requests)
		{
			str+=r + "\n";
		}
		return str;
	}
}
